package models

import (
	"time"

	"github.com/jinzhu/gorm"
)

type Feedback struct {
	Model
	Id       int    `json:"-"`
	Acid     int    `json:"acid"`
	Types    int    `json:"types"`
	Contents string `json:"contents"`
	Grade    int    `json:"grade"`
}

func (tag *Feedback) BeforeCreate(scope *gorm.Scope) error {
	scope.SetColumn("createtime", time.Now().Unix())
	return nil
}

func (tag *Feedback) BeforeUpdate(scope *gorm.Scope) error {
	scope.SetColumn("modifietime", time.Now().Unix())
	return nil
}

func GetFeedback(id int) (minds Feedback) {
	db.Where("id = ?", id).First(&minds)
	//db.Model(&minds).Related(&minds.Tag)
	return
}

func GetFeedbacks(PageNum int, PageSize int, maps interface{}) (minds []Feedback) {
	db.Where(maps).Offset(PageNum).Limit(PageSize).Find(&minds)
	return
}

func AddFeedback(data map[string]interface{}) bool {
	db.Create(&Feedback{
		Acid:    data["acid"].(int),
		Contents: data["contents"].(string),
		Types:   data["types"].(int),
		Grade:   data["grade"].(int),
	})
	return true
}

func EditFeedback(id int, maps interface{}) bool {
	db.Model(&Feedback{}).Where("id = ?", id).Update(maps)
	return true
}
func EditFeedbackSource(id string, maps interface{}) bool {
	db.Model(&Feedback{}).Where("version = 1 and pid = ?", id).Update(maps)
	return true
}

func DeleteFeedback(id int) bool {
	db.Where("id = ?", id).Delete(&Feedback{})
	return true
}

func ExistFeedbackByID(id int) bool {
	var minds Feedback
	db.Where("id = ?", id).First(&minds)
	if minds.ID > 0 {
		return true
	}
	return false
}

func ExistFeedbackByMindID(id int) bool {
	var minds Feedback
	db.Where("id = ?", id).First(&minds)
	if minds.ID > 0 {
		return true
	}
	return false
}

func GetFeedbackID(id string, version int) bool {
	var minds Feedback
	db.Where("version = ? and  pid = ?", version, id).First(&minds)
	if minds.ID > 0 {
		return true
	}
	return false
}

func GetFeedbackTotal(maps interface{}) (count int) {
	db.Model(&Feedback{}).Where(maps).Count(&count)
	return
}
